<template>
  <van-nav-bar
    :title="title"
    fixed
    :right-text="rightText"
    left-arrow
    @click-left="onClickLeft"
    @click-right="onClickRight"
  >
  </van-nav-bar>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
const props = defineProps<{
  title?: string
  rightText?: string
  back?: () => void
}>()

const emit = defineEmits<{
  (e: 'clickRight'): void
}>()
const router = useRouter()
// 点击左侧按钮
const onClickLeft = (): void => {
  if (props.back) return props.back()
  // 判断历史记录中是否有回退
  if (history.state?.back) {
    router.back()
  } else {
    router.push('/')
  }
}
// 点击右侧按钮
const onClickRight = (): void => {
  emit('clickRight')
}
</script>

<style lang="scss" scoped>
:deep() {
  .van-nav-bar {
    &__arrow {
      font-size: 18px;
      color: var(--cp-text1);
    }
    &__text {
      font-size: 15px;
    }
  }
}
</style>
